﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IceDog.SSHConnection.LocalCommand.Components;
using IceDog.SSHConnection.LocalCommand.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace IceDog.SSHConnection.LocalCommand.Pages
{
    public class CommandModel : PageModel
    {
        private SSHClient _sshClient;
        [BindProperty]
        public SSHClientInfo SSHClientInfo { get; set; }

        public void OnGet()
        {
            SSHClientInfo = new SSHClientInfo()
            {
                IP = "192.168.0.124",
                Port = 22,
                UserName = "root",
                Password = "edoc2@love00"//密码字段无法被赋值，只能在页面用value=的形式赋值
            };
        }

        public void OnPostConnecting()
        {
            try
            {
                if (_sshClient == null)
                {
                    _sshClient = new SSHClient(SSHClientInfo.IP, SSHClientInfo.Port, SSHClientInfo.UserName, SSHClientInfo.Password);
                }
                _sshClient.Connecting();
                SSHClientInfo.Response = "连接成功";
            }
            catch (Exception ex)
            {
                SSHClientInfo.Response = ex.Message;
            }
        }

        public void OnPostExecute()
        {
            var result = _sshClient.Execute(SSHClientInfo.Command);
            //SSHClientInfo.Response = result;
        }
    }
}